<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #create_time="{ record }">
        {{ tool.dateFormat(record.create_time, 'yyyy-MM-dd hh:mm:ss', '-') }}
      </template>
      <template #icon="{ record }">
        <a-avatar shape="square">
          <img :src="record.icon" style="object-fit: cover" />
        </a-avatar>
      </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import boxCategory from '@/api/box/boxCategory'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const crudRef = ref()


const numberOperation = (newValue, id, numberName) => {
  boxCategory.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  boxCategory.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'us_box_category',
  bordered:{ wrapper: true, cell: true },
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: boxCategory.getList,
  recycleApi: boxCategory.getRecycleList,
  add: {
    show: true,
    api: boxCategory.save,
    auth: ['box:category:save']
  },
  edit: {
    show: true,
    api: boxCategory.update,
    auth: ['box:category:update']
  },
  delete: {
    show: true,
    api: boxCategory.deletes,
    auth: ['box:category:delete'],
    realApi: boxCategory.realDeletes,
    realAuth: ['box:category:realDeletes']
  },
  recovery: {
    show: true,
    api: boxCategory.recoverys,
    auth: ['box:category:recovery']
  }
})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    width:40,
    addDisplay: false,
    editDisplay: false,
    commonRules: {
      required: true,
      message: "请输入ID"
    }
  },
  {
    title: "类型名称",
    width:260,
    dataIndex: "name",
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入类型名称"
    }
  },
  {
    title: "图标",
    dataIndex: "icon",
    formType: "upload",
    onlyData: true,
    returnType: "url",
    multiple: false,
    chunk: false,
    type: "image"
  },
  {
    title: "展示排序",
    dataIndex: "sort",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入展示排序"
    }
  },
  {
    title: "是否展示",
    dataIndex: "status",
    formType: "radio",
    dict: {
      name: "data_show_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    },
    search: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入创建者"
    }
  },
  {
    title: "创建时间",
    dataIndex: "create_time",
    width:180,
    formType: "range",
    search: true,
    addDisplay: false,
    editDisplay: false,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "update_time",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
</script>
<script> export default { name: 'box:category' } </script>